package vip.websky.project.core.log;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 日志切面
 *
 * @author: YangYong
 * @createAt: 2017/8/6 11:37
 */
@Aspect
@Component
@Slf4j
public class SystemLogAspect {
    /**
     * Service层切点
     */
    @Pointcut("@annotation(vip.websky.project.core.log.KingLog)")
    public void serviceAspect() {
    }

    @Before("serviceAspect()")
    public void doBefore(JoinPoint joinPoint) {
        log.info("调用【{}-{}】方法，请求参数：{}", getClassName(joinPoint), getClassFunnctionName(joinPoint), getParams(joinPoint));
    }

    @AfterReturning(returning = "response", pointcut = "serviceAspect()")
    public void doAfterReturning(JoinPoint joinPoint, Object response) {
        log.info("调用【{}-{}】方法，返回结果：{}", getClassName(joinPoint), getClassFunnctionName(joinPoint), response == null ? "" : response.toString());
    }


    private String getClassName(JoinPoint joinPoint) {
        return joinPoint.getTarget().getClass().getName();
    }

    private String getClassFunnctionName(JoinPoint joinPoint) {
        return joinPoint.getSignature().getName();
    }

    private String getParams(JoinPoint joinPoint) {
        return Arrays.toString(joinPoint.getArgs());
    }
}
